# -*- coding: utf-8 -*-
"""
Created on Wed Apr 10 10:07:09 2024

@author: 刘翼
"""

import cv2
import numpy as np

img = cv2.imread(r"C:\Users\Public\opencv\Figure\bird.jpg",0)
img = cv2.GaussianBlur(img,(3,3),0)   # 高斯滤波去噪

def nothing(x):
    pass

# 创建滑动窗口和滑动条
cv2.namedWindow('Canny',1)  
cv2.createTrackbar('minval','Canny',0,255,nothing)  
cv2.createTrackbar('maxval','Canny',0,255,nothing)

while True:
    # 读取滑动条数值
    minval = cv2.getTrackbarPos('minval','Canny')
    maxval = cv2.getTrackbarPos('maxval','Canny')
    edges = cv2.Canny(img,minval,maxval)

    # 拼接原图与边缘监测结果图
    img_2 = np.hstack((img,edges))
    cv2.imshow('Canny',img_2)

    key = cv2.waitKey(1) 
    if key == 27:  #按下Esc键，退出
        break    
cv2.destroyAllWindows()